package com.youdianstar.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.youdianstar.app.R;
import com.youdianstar.app.entity.CodeEntity;
import com.youdianstar.app.common.widget.adapter.BaseRecyclerAdapter;

import java.util.List;


/**
 * File: CountryCodeAdapter
 * Author: 82149 Create: 2021/9/27 15:01
 * Changes (from 2021/9/27)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 */
public class CountryCodeAdapter extends BaseRecyclerAdapter<CodeEntity> {
    private Context mContext;
    private LayoutInflater mInflater;

    public CountryCodeAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }
    /**
     * 提供给Activity刷新数据
     *
     * @param letterData selectList
     */
    public void setListViewData(List<CodeEntity> letterData) {
        addListData(letterData);
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_item_code, parent, false);
        return new CodeHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, CodeEntity data) {
        if (viewHolder instanceof CodeHolder) {
            String title = data.getTitle();
            if (!TextUtils.isEmpty(title) && !title.equals("-")) {
                ((CodeHolder) viewHolder).tvTitle.setVisibility(View.VISIBLE);
                ((CodeHolder) viewHolder).tvTitle.setText(title);
            } else {
                ((CodeHolder) viewHolder).tvTitle.setVisibility(View.GONE);
            }
            ((CodeHolder) viewHolder).tvName.setText(data.getAbb());
            ((CodeHolder) viewHolder).tvNum.setText("+" + data.getCode());
        }
    }

    /**
     * 获取制定位置数据
     */
    public Object getItem(int position) {
        return getDataList().get(position);
    }


    /**
     * 获取分类的首字母的char ascii值
     *
     * @param position index
     */
    public int getCharForPosition(int position) {
        return getDataList().get(position).getAbb().charAt(0);
    }


    class CodeHolder extends BaseRecyclerAdapter.Holder {
        TextView tvName;
        TextView tvNum;
        TextView tvTitle;
        ImageView ivLogo;

        public CodeHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.code_tv_Name);
            tvNum = itemView.findViewById(R.id.code_tv_Number);
            tvTitle = itemView.findViewById(R.id.code_title);
            ivLogo = itemView.findViewById(R.id.ivCountry);
        }
    }
}
